package com.naza.rpc.client;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.listener.Event;
import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.listener.NamingEvent;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.agent.model.NewService;
import com.ecwid.consul.v1.catalog.CatalogServiceRequest;
import com.ecwid.consul.v1.catalog.model.CatalogService;
import com.naza.rpc.discovery.DiscoveryListener;
import com.naza.rpc.model.ServiceInstance;
import com.naza.rpc.util.PropertiesUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * consul client
 *
 * @author yl
 */
public class ConsClient {

    private static Object LOCK = new Object();

    private ConsulClient consulClient;

    private static ConsClient INSTANCE = null;

    private String serverUrl = PropertiesUtil.getINSTANCE().getValue("register.addr");
    public static ConsClient getINSTANCE(){
        synchronized (LOCK){
            if(null == INSTANCE){
                synchronized (LOCK){
                    if(null == INSTANCE){
                        INSTANCE = new ConsClient();
                    }
                }
            }
        }
        return INSTANCE;
    }

    public ConsClient(){
        init();
    }
    public void init(){
        try {
            consulClient = new ConsulClient(serverUrl);
        }catch (Exception e){

        }
    }

    public void registerInstance(String serviceName,ServiceInstance instance) throws Exception {
        NewService newService = new NewService();
        newService.setId(instance.getId());
        List<String> tags = new ArrayList<String>();
        tags.add("0.0.0.1");
        newService.setTags(tags);
        newService.setName(instance.getServiceName());
        newService.setPort(instance.getPort());
        newService.setAddress(instance.getIp());
        NewService.Check serviceCheck = new NewService.Check();
        //health check
        serviceCheck.setHttp(instance.getIp()+":"+instance.getPort());
        serviceCheck.setInterval("10s");
        newService.setCheck(serviceCheck);
        consulClient.agentServiceRegister(newService);
    }

    public void deRegisterInstance(String serviceName,ServiceInstance instance) throws Exception {
        consulClient.agentServiceDeregister(instance.getId());
    }

    public List<Instance> getAllInstance(String serviceName) throws Exception {
        QueryParams params = QueryParams.Builder.builder().build();
        CatalogServiceRequest request = CatalogServiceRequest.newBuilder().setQueryParams(params).build();
        Response<List<CatalogService>> responses = consulClient.getCatalogService("testService1",request);
        List<CatalogService> services = responses.getValue();
        List<Instance> instances = new ArrayList<>();
        for(CatalogService catalogService : services){
            instances.add(createInstance(catalogService));
        }
        return instances;
    }

    public void monitor(String serviceName, DiscoveryListener listener) throws Exception {
        //not support
    }

    private Instance createInstance(CatalogService instance){
        Instance serviceInstance = new Instance();
        serviceInstance.setServiceName(instance.getServiceName());
        serviceInstance.setPort(instance.getServicePort());
        serviceInstance.setIp(instance.getAddress());
        serviceInstance.setInstanceId(instance.getServiceId());
        return serviceInstance;
    }
}
